//
//  AdvertiserDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/9.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AdvertiserDetailViewController.h"
#import "HttpOrgAction.h"
#import "ShopTypeTableViewCell.h"
#import "ShopTypeDetailViewController.h"
#import "HttpContractAction.h"

@interface AdvertiserDetailViewController ()
@property (nonatomic,strong) OrgInfo *orgInfo;
@property (nonatomic,strong) NSArray *listArray;
@property (nonatomic,strong) UILabel *integralLabel;
@end

@implementation AdvertiserDetailViewController

-(id)initWithOrgInfo:(OrgInfo*)orgInfo{
    if (self == [super init]) {
        self.orgInfo = orgInfo;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = [self getHeaderView];
    self.tableView.separatorStyle = 0;
    [self loadMachineNumber];
    [self loadData];
}

-(void)loadMachineNumber{
    NSDictionary *params = @{@"orgId":self.orgInfo.id};
    [HttpContractAction getContractTotalInfo:params block:^(id result, NSError *error) {
        if (!error) {
            NSDictionary *dic = result;
            self.integralLabel.text = [NSString stringWithFormat:@"机器数量\n%d",[dic[@"totalEquipmentNumber"] intValue]];
        }
    }];
}

-(UIView*)getHeaderView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    view.backgroundColor = HexRGB(0xF48D55);
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.text = self.orgInfo.name;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [view addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(view, 0).topSpaceToView(view, StatusBarHeight+44).widthIs(SCREEN_WIDTH).heightIs(20);
    
    UIButton *addressButton = [UIButton new];
    addressButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [addressButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addressButton setTitle:[NSString stringWithFormat:@"%@%@%@%@",self.orgInfo.province?self.orgInfo.province:@"",self.orgInfo.city,self.orgInfo.area,self.orgInfo.address] forState:UIControlStateNormal];
    [addressButton setImage:[UIImage imageNamed:@"machine_local"] forState:UIControlStateNormal];
    [view addSubview:addressButton];
    addressButton.sd_layout.leftSpaceToView(view, 20).topSpaceToView(titleLabel, 10).widthIs(SCREEN_WIDTH-40).heightIs(20);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor whiteColor];
    [view addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(view, 0).bottomSpaceToView(view, 75).rightSpaceToView(view, 0).heightIs(1);
    
    UILabel *totalEarnLabel = [UILabel new];
    totalEarnLabel.text = @"现有合同总金额\n3000";
    totalEarnLabel.textColor = [UIColor whiteColor];
    totalEarnLabel.textAlignment = NSTextAlignmentCenter;
    totalEarnLabel.numberOfLines = 0;
    totalEarnLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:totalEarnLabel];
    totalEarnLabel.sd_layout.leftSpaceToView(view, 0).bottomSpaceToView(view, 0).heightIs(74).widthIs(SCREEN_WIDTH/2);
    
    UILabel *integralLabel = [UILabel new];
    integralLabel.text = @"机器数量\n3000";
    integralLabel.textColor = [UIColor whiteColor];
    integralLabel.textAlignment = NSTextAlignmentCenter;
    integralLabel.numberOfLines = 0;
    integralLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:integralLabel];
    integralLabel.sd_layout.leftSpaceToView(totalEarnLabel, 0).bottomSpaceToView(view, 0).heightIs(74).widthIs(SCREEN_WIDTH/2);
    self.integralLabel = integralLabel;
    
    return view;
}

-(void)loadData{
    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
    [HttpOrgAction getOrgContractList:self.orgInfo.id block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            NSArray *array = result;
            self.listArray = array;
            [self.tableView reloadData];
            if (array.count == 0) {
//                [self showNoResultViewWithView:self.tableView];
                
            }
        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ShopTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ShopTypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    MerchantContractInfo *merchantContractInfo = self.listArray[indexPath.row];
    [cell setShopTypeCell:merchantContractInfo];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MerchantContractInfo *merchantContractInfo = self.listArray[indexPath.row];
    ShopTypeDetailViewController *shopTypeDetailViewController = [[ShopTypeDetailViewController alloc] initWithShopTypeInfo:merchantContractInfo Block:^{
        [self loadData];
    }];
    [self.navigationController pushViewController:shopTypeDetailViewController animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
